//
//  InspectionController.m
//  ZZmetro
//
//  Created by 一介布衣 on 2020/6/24.
//  Copyright © 2020 一介布衣. All rights reserved.
//

#import "InspectionController.h"
#import "InspectionCell.h"
#import "UnFinishInspectionRequest.h"
#import "UnFinshInspecion.h"
#import "InspectionLineRequest.h"
#import "InspectionLine.h"
#import "InspectionDetailRequest.h"
#import "InspectionDetail.h"
#import "InspectionLineView.h"
#import "BottomSheet.h"

@interface InspectionController () <UITableViewDelegate, UITableViewDataSource>

    @property(nonatomic, strong) UIView *lineSelectView;
    @property(nonatomic, strong) UIView *inpectionDetailView;
    @property(nonatomic, strong) UITableView *tableView;

    @property(nonatomic, strong) InspectionDetail* inpectionPoints;
@end

@implementation InspectionController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"车站巡视";
    [self setupBg];
    
    [self loadUnfinishInspection];
    //[self setupLineSelectView];
    //[self setupDetailView];
    //[self setupInspectionPointsView];
}

- (void) loadUnfinishInspection {
    UnFinishInspectionRequest* request = [[UnFinishInspectionRequest alloc]init];
    [HttpRequestOperation request:request quite:NO callBack:^(HttpResponse * _Nullable response) {
        [[response failureDefalut] success:^(UnFinshInspecion*  _Nonnull data) {
            //[Toast toast:data.joinedTourLineid];
            if (data.joinedTourLineid == nil || data.joinedTourLineid == NULL ||
                [@"" isEqual:data.joinedTourLineid] || data.joinedTourLineid.length == 0) {
                //empty str
                [self setupLineSelectView];
            } else {
                //[self setupInspectionPointsView];
                [self loadInspectionPointsByLineId:data.joinedTourLineid];
            }
        }];
    }];
}

- (void) loadInspectionLine {
    InspectionLineRequest* request = [[InspectionLineRequest alloc]init];
    [HttpRequestOperation request:request quite:NO callBack:^(HttpResponse * _Nullable response) {
        [[response failureDefalut] success:^(NSArray *  _Nonnull data) {
            //NSLog(@"data size=%i", data.count);
            //InspectionLine *lineInfo = data[0];
            //[Toast toast:lineInfo.name];
            
            [self showLineView: data];
        }];
    }];
}

-(void) showLineView:(NSArray <InspectionLine *> *) inspectionLines {
    InspectionLineView* view = [[InspectionLineView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 260)];
    view.inspecionLines = inspectionLines;
    BottomSheet* bt = [[BottomSheet alloc] init];
    bt.builder = [BottomSheet sheetBuilder];
    bt.builder.hideTitle = YES;
    bt.builder.sheetHeight = view.frame.size.height;
    [bt setBootSheetContentView:view];
    [bt show:self];
    
    view.selectBtn = ^(BOOL confirm, InspectionLine * line) {
        [bt dissView];
        
        if (confirm && line != nil) {
            [Toast toast:line.name];
            
            [self loadInspectionPointsByLineId:line.id];
        }
    };
   
}

- (void) loadInspectionPointsByLineId:(NSString*) lineId {
    InspectionDetailRequest* request = [[InspectionDetailRequest alloc]init];
    request.lineid = lineId;
    [HttpRequestOperation request:request quite:NO callBack:^(HttpResponse * _Nullable response) {
        [[response failureDefalut] success:^(InspectionDetail*  _Nonnull data) {
            self.inpectionPoints = data;
            self.lineSelectView.hidden = YES;
            
            [self setupDetailView];
            [self setupInspectionPointsView];
        }];
    }];
}

- (void) setupDetailView {
    _inpectionDetailView = [self getItemSelectView:@"巡视详情"];
    [self.view addSubview:_inpectionDetailView];
       
   [_inpectionDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.bottom.equalTo(self.view).offset(-16);
       make.left.equalTo(self.view).offset(16);
       make.width.equalTo(self.view);
       make.height.mas_equalTo(60);
   }];
       
   [_inpectionDetailView addGestureRecognizer:GestureType_Tap block:^(__kindof UIView * _Nonnull view, __kindof UIGestureRecognizer * _Nonnull gesture) {
         [Toast toast:@"click detail"];
     }];
}

- (void) setupLineSelectView {
    _lineSelectView = [self getItemSelectView:@"选择线路"];
     [self.view addSubview:_lineSelectView];
        
    [_lineSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(16);
        make.left.equalTo(self.view).offset(16);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(60);
    }];
        
    CommonWeakSelf;
    [_lineSelectView addGestureRecognizer:GestureType_Tap block:^(__kindof UIView * _Nonnull view, __kindof UIGestureRecognizer * _Nonnull gesture) {
          //[Toast toast:@"click"];
        
        CommonStrongSelf;
        [strongSelf loadInspectionLine];
    }];
}

- (void) setupBg {
    UIView* line = [[UIView alloc] init];
    line.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0.55);
    line.backgroundColor = UIColor.whiteColor;
    
    UIView* blueBg = [[UIView alloc] init];
    blueBg.frame = CGRectMake(0, 0.55, SCREEN_WIDTH, 64);
    blueBg.backgroundColor = HEXCOLOR(0X5A7EEE);
    
    [self.view addSubview:line];
    [self.view addSubview:blueBg];
}

- (UIView*) getItemSelectView:(NSString*) str {
    int offsetX = 16;
    int height = 56;
    UIView* itemView = [[UIView alloc] init];
    //itemView.frame = CGRectMake(offsetX, 20, SCREEN_WIDTH - 2 * offsetX, height);
    
    //NSString *path = [[NSBundle mainBundle] pathForResource:@"bg_welcome"ofType:@"jpeg"];
    //UIImage *image = [UIImage imageWithContentsOfFile:path];
    //_lineSelectView.layer.contents = (id)image.CGImage;
    //_lineSelectView.backgroundColor = [UIColor whiteColor];
    
    itemView.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.1).CGColor;
    itemView.layer.shadowOffset = CGSizeMake(0, 2);
    itemView.layer.shadowOpacity = 1;
    itemView.layer.shadowRadius = 3;
    itemView.layer.cornerRadius = 4;
    
    UIView* view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH - 2 * offsetX, height);
    view.backgroundColor = [UIColor whiteColor];
    [view.layer setMasksToBounds:YES];
    [view.layer setCornerRadius:4];
    view.clipsToBounds = YES;
    
    UIImageView* imgView = [[UIImageView alloc] init];
    UIImage* img = [UIImage imageNamed:@"ic_right_arrow"];
    imgView.image = img;
    
    UILabel* label = [[UILabel alloc] init];
    UIFont *font = [UIFont systemFontOfSize:15];
    label.text = str;
    label.textAlignment = NSTextAlignmentRight;
    label.textColor = [UIColor blackColor];
    label.font = font;
    
    [itemView addSubview:view];
    [itemView addSubview:imgView];
    [itemView addSubview:label];
    //[self.view addSubview:itemView];
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(itemView).offset(-44);
        make.top.equalTo(itemView).offset(20);
    }];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(itemView).offset(-60);
        make.left.equalTo(itemView).offset(16);
        make.top.equalTo(itemView).offset(19);
    }];
    
    return itemView;
}

- (void) setupInspectionPointsView {
    UIView* itemView = [[UIView alloc] init];
    itemView.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.1).CGColor;
    itemView.layer.shadowOffset = CGSizeMake(0, 2);
    itemView.layer.shadowOpacity = 1;
    itemView.layer.shadowRadius = 3;
    itemView.layer.cornerRadius = 4;
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight) style:UITableViewStylePlain];
    [_tableView.layer setMasksToBounds:YES];
    [_tableView.layer setCornerRadius:4];
    _tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
    _tableView.clipsToBounds = YES;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellEditingStyleNone; //隐藏分割线
    //_tableView.backgroundColor = [UIColor redColor];
    //_tableView.estimatedRowHeight = 100;
    _tableView.rowHeight = 100;
    [self.tableView registerNib:[UINib nibWithNibName:@"InspectionCell" bundle:nil] forCellReuseIdentifier:@"InpectionIdentifier"];
    
    [itemView addSubview:_tableView];
    [self.view addSubview:itemView];
    
    [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(16);
        make.left.equalTo(self.view).offset(16);
        make.bottom.equalTo(self.inpectionDetailView.mas_top).offset(-4);
        make.width.mas_equalTo(SCREEN_WIDTH - 32);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemView).offset(10);
        make.left.equalTo(itemView).offset(0);
        make.bottom.equalTo(itemView).offset(-10);
        make.right.equalTo(itemView).offset(0);
    }];
    
}

#define mark --UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.inpectionPoints.codeList.count;
    //return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InspectionCell* cell = (InspectionCell*) [tableView dequeueReusableCellWithIdentifier:@"InpectionIdentifier"];
    CommonWeakSelf;
    cell.dataChangedblock = ^(InspectionPoint * _Nonnull insPoint, NSInteger index) {
        CommonStrongSelf;
        [strongSelf.inpectionPoints.codeList setObject:insPoint atIndexedSubscript:index];
        [strongSelf.tableView reloadData];
        
    };
    InspectionPoint* point = _inpectionPoints.codeList[indexPath.row];
    [cell bindData:point withIndex:indexPath.row];
    return cell;
}

@end
